package com.example.demo.service.impl;

import com.example.demo.entity.Online;
import com.example.demo.entity.OnlineProject;
import com.example.demo.exception.NotFoundException;
import com.example.demo.mapper.OnlineProjectMapper;
import com.example.demo.service.OnlineProjectService;
import com.example.demo.service.OnlineService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 叮当猫的百宝箱
 * @since 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Lazy))
public class OnlineProjectServiceImpl implements OnlineProjectService {


    private final OnlineProjectMapper onlineProjectMapper;

    private final OnlineService onlineService;

    /**
     * 添加新的网签项目配置
     *
     * @param onlineProject 待添加的网签配置信息
     */
    @Override
    public void createOnlineProject(OnlineProject onlineProject) {
        Online online = onlineService.getOnlineById(onlineProject.getOnlineId());
        if (online == null) {
            throw new NotFoundException("网签配置不存在");
        }
        onlineProjectMapper.insert(onlineProject);
    }

    /**
     * 根据网签项目配置ID删除一个网签项目配置
     *
     * @param id 网签项目配置ID
     */
    @Override
    public void deleteOnlineProjectById(Long id) {
        onlineProjectMapper.deleteById(id);
    }

    /**
     * 根据网签配置ID删除所属网签配置的全部网签项目配置信息
     *
     * @param onlineId 网签配置ID
     */
    @Override
    public void deleteOnlineProjectByOnlineId(Long onlineId) {
        onlineProjectMapper.deleteByOnlineId(onlineId);
    }

    /**
     * 根据网签项目配置ID修改网签项目配置信息
     *
     * @param id            网签项目配置ID
     * @param onlineProject 待更新的网签项目配置信息
     */
    @Override
    public void updateOnlineProjectById(Long id, OnlineProject onlineProject) {
        onlineProjectMapper.updateSelectiveById(id, onlineProject);
    }

    /**
     * 根据网签项目配置ID,获取网签项目配置的详细信息
     *
     * @param id 网签项目配置ID
     * @return 网签项目配置的详细信息
     */
    @Override
    public OnlineProject getOnlineProjectById(Long id) {
        return onlineProjectMapper.findById(id);
    }

    /**
     * 根据网签配置ID获取，网签配置下的所有网签配置项目信息
     *
     * @param onlineId 网签配置ID
     * @return 所属网签配置的所有网签项目信息列表
     */
    @Override
    public List<OnlineProject> getOnlineProjectByOnlineId(Long onlineId) {
        return onlineProjectMapper.findByOnlineId(onlineId);
    }
}
